Ext.onReady(function(){ 

 Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'事件类型',dataIndex:'eventExtraType',width:150},
{header:'事件描述',dataIndex:'eventDesc',width:300}
]); 

var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listEvents.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'eventExtraType'}, 
{name:'eventDesc'}
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var yesOrNoStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=YesNo',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
yesOrNoStore.load();

var eventsExtraTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=EventExtraType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	}); 
	eventsExtraTypeStore.load();

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"客户重大事项列表", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:check},
 '-',
 {text:'删除',handler:del}
 ]              
});

//新增表单
addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 480, 
height: 250, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 60, 
items: 
[ { xtype: 'fieldset', 
title: '重大事项信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ { layout : "form",
           items : [{
xtype: 'combo', 
 id : "eventExtraType",
              store :eventsExtraTypeStore,
              fieldLabel: '事件类型',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择事件类型...',
              allowBlank : false,
              blankText:'请选择事件类型',
              typeAhead : true,
              width: 362,
              mode:'remote',  
              hiddenName:'eventExtraType',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true
}]},
{ layout : "form",
           items : [{
xtype: 'textarea',
id: 'eventDesc', 
name: 'eventDesc', 
fieldLabel: '事件描述', 
emptyText: '必填', 
blankText: '事件描述不能为空', 
allowBlank: false,  
maxLength:100,
maxLengthText:'最多只允许输入100个汉字',
height:100,
anchor: '97%' 
}]},
{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '重大事项流水号',
              allowBlank: true
}]},
]}]});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '客户-重大事项', 
width: 500, 
height: 290, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function addFunction() {

	
	var addForm = Ext.getCmp("addForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
	 url : 'customer/addEvents.do', 
	 method : 'post', 
	 success : function() { 
	 grid.store.reload();
	 Ext.Msg.alert('提示', '保存成功！'); 
	 }, failure : function() { 
	  Ext.Msg.alert('提示', '保存失败！'); 
	  } 
});  
}
 }
  


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addWin").hide(); 
	
Ext.getCmp('addForm').form.reset(); 
Ext.getCmp("addWin").buttons[0].handler = addFunction; 
Ext.getCmp("addWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function del(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的内容！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
//alert(grid.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteEvents.do',
  params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   grid.store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//编辑地址
function editFunction(){
	
	var addForm = Ext.getCmp("addForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
                    url : 'customer/editEvents.do', 
                    method : 'post', 
                       success : function() { 
                       grid.store.reload();
                       Ext.Msg.alert('提示', '编辑成功！'); 
                       }, 
                        failure : function() { 

                           Ext.Msg.alert('提示', '编辑失败！'); 
 
                        } 
                    }); 
                    }
}

function check(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的内容！"); 
}
else{
	var serialNo = grid.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addForm').form.reset(); 
	Ext.getCmp("addWin").buttons[0].handler = editFunction; 

	Ext.Ajax.request({
url:'customer/getEvents.do',
params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
  success:function(response){
 // alert(response.responseText);
 var jo = Ext.util.JSON.decode(response.responseText);
  	Ext.getCmp("serialNo").setValue(jo.serialNo);
	Ext.getCmp("eventExtraType").setValue(jo.eventExtraType);
  	Ext.getCmp("eventDesc").setValue(jo.eventDesc);
  	
	Ext.getCmp("addWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}
grid.addListener('rowdblclick',check);
}); 